package com.suitsoft.bdms.web;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ServletCom {

 private static final long serialVersionUID = 1L;

 public static void main(String[] args) {
	 upLoad();
 }
 protected static void upLoad() {
		System.out.println("调用了upLoad()......");
		try {
			String BOUNDARY = "---------7d4a6d158c9";

			URL url = new URL(
					"http://192.168.1.167:8080/hrq/received/wacker!sm.action");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			conn.setDoOutput(true);

			conn.setDoInput(true);

			conn.setUseCaches(false);

			conn.setRequestMethod("POST");

			conn.setRequestProperty("connection", "Keep-Alive");

			conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

			conn.setRequestProperty("Charsert", "UTF-8");

			conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);
			OutputStream out = new DataOutputStream(conn.getOutputStream());

			byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
//			String filename = (String) this.mSourcesCombo1.getSelectedItem();
			
		//	String filename = (String) this.username.getText();
//			System.out.println("filename.trim()" + filename.trim());
			File file = new File("d:\\良奇于.JPG");
			System.out.println(file.getPath());
			StringBuilder sb = new StringBuilder();
			sb.append("--");
			sb.append(BOUNDARY);
			sb.append("\r\n");
			sb.append("Content-Disposition: form-data;name=\"uploadFile\";filename=\""+ file.getName() + "\"\r\n");

			sb.append("Content-Type:application/octet-stream\r\n\r\n");

			byte[] data = sb.toString().getBytes();

			out.write(data);

			DataInputStream in = new DataInputStream(new FileInputStream(file));

			int bytes = 0;

			byte[] bufferOut = new byte[1024];

			while ((bytes = in.read(bufferOut)) != -1) {
				out.write(bufferOut, 0, bytes);
			}

			out.write("\r\n".getBytes());

			in.close();

			out.write(end_data);

			out.flush();

			out.close();

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));

			String line = null;

			while ((line = reader.readLine()) != null) {
				System.out.println(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
 public static void oldUpload() {

	   try {

	   String BOUNDARY = "---------7d4a6d158c9"; // 定义数据分隔线

	   URL url = new URL("http://me:8080/ha/mult-applet!receviedFile.action");

	   HttpURLConnection conn = (HttpURLConnection) url.openConnection();

	   // 发送POST请求必须设置如下两行

	   conn.setDoOutput(true);

	   conn.setDoInput(true);

	   conn.setUseCaches(false);

	   conn.setRequestMethod("POST");

	   conn.setRequestProperty("connection", "Keep-Alive");

	   conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

	   conn.setRequestProperty("Charsert", "UTF-8");

	   conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

	   OutputStream out = new DataOutputStream(conn.getOutputStream());

	   byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线


	   File file = new File("D:\\desk\\a\\A0000000001-001.tif");

	   StringBuilder sb = new StringBuilder();

	   sb.append("--");

	   sb.append(BOUNDARY);

	   sb.append("\r\n");

	   sb.append("Content-Disposition: form-data;name=\"uploadFile\";filename=\""+ file.getName() + "\"\r\n");//这里的name就是action里的File uploadFile

	   sb.append("Content-Type:application/octet-stream\r\n\r\n");

	   byte[] data = sb.toString().getBytes();

	   out.write(data);

	   DataInputStream in = new DataInputStream(new FileInputStream(file));

	   int bytes = 0;

	   byte[] bufferOut = new byte[1024];

	   while ((bytes = in.read(bufferOut)) != -1) {

	   out.write(bufferOut, 0, bytes);

	   }

	   out.write("\r\n".getBytes()); //多个文件时，二个文件之间加入这个

	   in.close();


	   out.write(end_data);

	   out.flush();

	   out.close();

	   // 定义BufferedReader输入流来读取URL的响应

	   BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

	   String line = null;

	   while ((line = reader.readLine()) != null) {

	   System.out.println(line);

	   }

	   } catch (Exception e) {

	   System.out.println("发送POST请求出现异常！" + e);

	   e.printStackTrace();

	   }
	 }


}

